<?php

class edit_orders extends a_admin_controller {

    private $order_service;
    private $order_object;
    private $order_msg;
    private $address_data;
    private $extra_data;

    public function __construct() {
        parent::__construct();
        $this->order_service = new service_l_order();
        
    }

    public function order_id($id=NULL) {
        $order = $this->order_service->get_object($id);        
        $this->__setup_form_data($id);
    }
    public function batch_assign() {
        $this->assign("order", $this->order_object);
        $this->assign("order_edit_msg", $this->order_msg);
        $this->assign("form_data", $this->address_data);
        $this->assign("extra_data", $this->extra_data);
        

    }

    // <editor-fold desc="address and extra information generation">
    protected function __get_saved_extra_data() {

        $extra = $this->order_service->get_extra_data($this->order_object->id);
        $arr_data = array();

        if ($extra) {
            foreach ($extra as $e) {
                $arr_data[$e->get_field_details()->field_name] = $e->field_data;
            }
        }

        return $arr_data;
    }

    protected function get_extra_fields(order_m_order $order_obj) {
        $saved = $this->__get_saved_extra_data();
        $obj = new details_fields_type_m_order();
        foreach (array(details_fields_type_m_order::EXTRA => $obj->get_desc(details_fields_type_m_order::EXTRA)) as $key => $desc) {

            $field_obj = new details_fields_m_order();
            $fields = $field_obj->where("type", $key)->where('visible', '1')->order_by("order")->get();
            if ($fields->result_count() > 0) {
                $this->form->open("order/admin/edit_orders/edit_extra_info/".$order_obj->id, 'extra_info_form');
                $this->form->fieldset($desc);
                foreach ($fields as $field) {
                    $default_value = isset($saved[$field->field_name]) ? $saved[$field->field_name] : $field->default_value;
                    switch ($field->rendering_class) {
                        case "text":
                            $this->form->text($field->field_name, $field->field_description, $field->validation_rule, $default_value, array('title' => $field->field_note));
                            break;
                        case "textarea":
                            $this->form->textarea($field->field_name, $field->field_description, $field->validation_rule, $default_value, array('title' => $field->field_note));
                            break;
                        case "radiogroup":
                            $radios = array();
                            $pieces = explode(";", $field->values);
                            foreach ($pieces as $piece)
                                $radios[] = explode("=", $piece);
                            $this->form->radiogroup($field->field_name, $radios, $field->field_description, $default_value, $field->validation_rule, array('title' => $field->field_note));
                            break;
                        case "checkbox":
                            $this->form->br();
                            $this->form->checkbox($field->field_name, $field->values, $field->field_description, $default_value == "checked" ? TRUE : FALSE, $field->validation_rule, array('title' => $field->field_note));
                            break;
                        case "checkgroup":
                            $options = array();
                            $pieces = explode(";", $field->values);
                            foreach ($pieces as $piece)
                                $options[] = explode("=", $piece);
                            $this->form->checkgroup($field->field_name, $options, $field->field_description, $default_value, $field->validation_rule, array('title' => $field->field_note));
                            break;
                        case "select_one" :
                            $options = array();
                            $pieces = explode(";", $field->values);
                            foreach ($pieces as $piece) {
                                $even_more_pieces = explode("=", $piece);
                                $options[$even_more_pieces[0]] = $even_more_pieces[1];
                            }
                            $this->form->select($field->field_name, $options, $field->field_description, $default_value, $field->validation_rule, array('title' => $field->field_note));
                            break;
                        default:
                            break;
                    }
                    if (strlen($field->field_error_msg) > 0) {
                        $this->form->set_error($field->field_name, $field->field_error_msg);
                    }
                }
            }
        }
        
        $this->form->indent(150);
        //$this->form->hidden(i_handle_posts::OP_CODE,"create_user");
        $this->form->submit('Update','submit_extra');
        $this->extra_data[] = $this->form->get();
    }

    protected function __get_saved_address() {

        $billing = $this->order_service->get_billing_address($this->order_object->id);
        $shipping = $this->order_service->get_shiping_address($this->order_object->id);
        $extra = $this->order_service->get_extra_data($this->order_object->id);
        $arr_data = array();

        if ($billing) {
            foreach ($billing->fields as $b) {
                $arr_data["billing_" . $b] = $billing->$b;
            }
        } else {

        }
        if ($shipping) {
            foreach ($shipping->fields as $s) {
                $arr_data["shipping_" . $s] = $shipping->$s;
            }
        } else {

        }
//        if ($extra) {
//            foreach ($extra as $e) {
//                $arr_data[$e->get_field_details()->field_name] = $e->field_data;
//            }
//        }

        return $arr_data;
    }

    protected function get_address_fields(order_m_order $order_obj) {
        $saved = $this->__get_saved_address();
        $obj = new details_fields_type_m_order();
        foreach ($obj->get_address_types() as $key => $desc) {

            $field_obj = new details_fields_m_order();
            $fields = $field_obj->where("type", $key)->where('visible', '1')->order_by("order")->get();
            if ($fields->result_count() > 0) {
                $this->form->open("order/admin/edit_orders/edit_address_info/".$order_obj->id, 'address_info_form');
                $this->form->fieldset($desc);
                foreach ($fields as $field) {
                    $default_value = isset($saved[$field->field_name]) ? $saved[$field->field_name] : $field->default_value;
                    switch ($field->rendering_class) {
                        case "text":
                            $this->form->text($field->field_name, $field->field_description, $field->validation_rule, $default_value, array('title' => $field->field_note));
                            break;
                        case "textarea":
                            $this->form->textarea($field->field_name, $field->field_description, $field->validation_rule, $default_value, array('title' => $field->field_note));
                            break;
                        case "radiogroup":
                            $radios = array();
                            $pieces = explode(";", $field->values);
                            foreach ($pieces as $piece)
                                $radios[] = explode("=", $piece);
                            $this->form->radiogroup($field->field_name, $radios, $field->field_description, $default_value, $field->validation_rule, array('title' => $field->field_note));
                            break;
                        case "checkbox":
                            $this->form->br();
                            $this->form->checkbox($field->field_name, $field->values, $field->field_description, $default_value == "checked" ? TRUE : FALSE, $field->validation_rule, array('title' => $field->field_note));
                            break;
                        case "checkgroup":
                            $options = array();
                            $pieces = explode(";", $field->values);
                            foreach ($pieces as $piece)
                                $options[] = explode("=", $piece);
                            $this->form->checkgroup($field->field_name, $options, $field->field_description, $default_value, $field->validation_rule, array('title' => $field->field_note));
                            break;
                        case "select_one" :
                            $options = array();
                            $pieces = explode(";", $field->values);
                            foreach ($pieces as $piece) {
                                $even_more_pieces = explode("=", $piece);
                                $options[$even_more_pieces[0]] = $even_more_pieces[1];
                            }
                            $this->form->select($field->field_name, $options, $field->field_description, $default_value, $field->validation_rule, array('title' => $field->field_note));
                            break;
                        default:
                            break;
                    }
                    if (strlen($field->field_error_msg) > 0) {
                        $this->form->set_error($field->field_name, $field->field_error_msg);
                    }
                }
            }
        }

        $this->form->indent(150);
        //$this->form->hidden(i_handle_posts::OP_CODE,"create_user");       
        $this->form->submit('Update','submit_address');
        $this->address_data[] = $this->form->get();
    }

    // </editor-fold>
    
    // <editor-fold desc="order items related functions">
    public function add_item(){
        $part_num = $this->input->post('txt_ipnum');
        $quantity = $this->input->post('txt_iqty');
        $order_id = $this->input->post('order_id');
        $product = new product_m_product();
        $product->get_by_part_num($part_num);
        if(is_numeric($quantity) && $quantity>0){
            if($product->exists()){
                if($this->order_service->item_exists($order_id, $part_num)){
                    $this->order_service->update_item($order_id, $part_num, $quantity);
                }else{
                    $this->order_service->add_an_item($product, $order_id, $quantity);
                }
            }
        }
        $this->set_ui_tab_index(2);
        redirect("order/admin/edit_orders/order_id/$order_id");
        
    }
    public function remove_item($order_id,$item_id){
        $this->order_service->remove_item($item_id);
        $this->set_ui_tab_index(2);
        redirect("order/admin/edit_orders/order_id/$order_id");
    }
    public function update_item(){
        $item_id = post('item_id');
        $qty     = post('iqty');
        if($item_id && $qty){
            if(is_numeric($item_id) && is_numeric($qty) && $qty>0){                
                $this->order_service->update_item_qty($item_id, $qty,FALSE);
            }
        }
        $this->set_ui_tab_index(2);
        redirect("order/admin/edit_orders/order_id/".  post('order_id'));
    }
    // </editor-fold>

    // <editor-fold desc="address and extra information edit/update">
        public function edit_extra_info($id){
            $order = $this->__setup_form_data($id);
            if($order && $this->form->valid){
                $this->order_service->save_extra_data($order->id, (object)$this->form->get_post());
            }
            $this->set_ui_tab_index(5);

        }
        public function edit_address_info($id){
            $order = $this->__setup_form_data($id);
            if($order && $this->form->valid){
                $this->order_service->save_shipping_address($order->id, (object)$this->form->get_post());
            }
            $this->set_ui_tab_index(1);
        }

        private function __setup_form_data($id){
            $order = $this->order_service->get_object($id);
            if ($order !== NULL) {
                $this->order_object = $order;
                $this->get_address_fields($this->order_object);
                $this->form->clear();
                $this->get_extra_fields($this->order_object);
                return $this->order_object;    

            } else {
                $this->order_object = NULL;
                $this->order_msg = "Invalid order ID";
                return NULL;
            }
        }
    // </editor-fold>

    //<editor-fold desc="Order Discount">
    public function add_discount(){
        $order_id = post("order_id");
        $amount = post("discount_amount");
        $desc   = post("discount_desc");
        $type   = post("discount_type");
        if(is_numeric($amount) && ($desc && strlen(trim($desc)))){
            $this->order_service->add_discount($order_id, $desc, $amount,$type);
        }
        $this->set_ui_tab_index(6);
       redirect("order/admin/edit_orders/order_id/$order_id");
    }

    public function delete_discount(){
        $discount_id = post("discount_id");
        $order_id = post("order_id");
        if($discount_id){
            $this->order_service->delete_discount_by_id($discount_id);
        }
        $this->set_ui_tab_index(6);
       redirect("order/admin/edit_orders/order_id/$order_id");
    }
    //</editor-fold>

    //<editor-fold desc="Order Misc Costs">
    public function add_misc_cost(){
        $order_id = post("order_id");
        $amount = post("misc_amount");
        $desc   = post("misc_desc");        
        if(is_numeric($amount) && ($desc && strlen(trim($desc)))){
            $this->order_service->add_misc_cost($order_id, $desc, $amount);
        }
        $this->set_ui_tab_index(7);
       redirect("order/admin/edit_orders/order_id/$order_id");
    }

    public function delete_misc_cost(){
        $misc_id = post("misc_id");
        $order_id = post("order_id");
        if($misc_id){
            $this->order_service->delete_misc_cost_by_id($misc_id);
        }
        $this->set_ui_tab_index(7);
       redirect("order/admin/edit_orders/order_id/$order_id");
    }
    //</editor-fold>
}

?>